<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { examScoreStatistics, tankChart } from '@/api/plan/plan'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      scoreData:null,
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      examScoreStatistics().then(response => {
        this.scoreData = response.data;

        const examNames = this.scoreData.map(stat => stat.examName);
        const percent1 = this.scoreData.map(stat => stat.percentOne);
        const percent2 = this.scoreData.map(stat => stat.percentTwo);
        const percent3 = this.scoreData.map(stat => stat.percentOne);

        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: 10,
            left: '2%',
            right: '2%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: examNames||['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            axisTick: {
              show: false
            }
          }],
          series: [{
            name: '得分率0~60',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: percent1||[79, 52, 200, 334, 390, 330, 220],
            animationDuration
          }, {
            name: '得分率60~80',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: percent2||[80, 52, 200, 334, 390, 330, 220],
            animationDuration
          }, {
            name: '得分率80~100',
            type: 'bar',
            stack: 'vistors',
            barWidth: '60%',
            data: percent3||[30, 52, 200, 334, 390, 330, 220],
            animationDuration
          }]
        })

      }).catch(error => {
        console.error('获取数据失败:', error);
      });

    }
  }
}
</script>
